package com.yufubao.service.service.applet.discounts.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yufubao.framework.util.CopyBean;
import com.yufubao.framework.util.page.Query;
import com.yufubao.framework.result.PageUtils;
import com.yufubao.model.dto.applet.discounts.SkuDiscountsEtcDto;
import com.yufubao.model.pojo.applet.discounts.SkuFullReductionEntity;
import com.yufubao.model.pojo.applet.discounts.SkuLadderEntity;
import com.yufubao.service.mapper.applet.discounts.SkuFullReductionDao;
import com.yufubao.service.service.applet.discounts.SkuFullReductionService;
import com.yufubao.service.service.applet.discounts.SkuLadderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;



@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * @param skuDiscountsEts
     * @return void
     * @description 保存满减信息
     * @author zhangyiheng
     * @date 2024/8/29 17:15
     */
    @Override
    public boolean saveSkuDiscountsEtc(List<SkuDiscountsEtcDto> skuDiscountsEts) {
        if (CollUtil.isNotEmpty(skuDiscountsEts)) {
            for (SkuDiscountsEtcDto skuDiscountsEtc : skuDiscountsEts) {
                //sms_sku_Ladder
                if (skuDiscountsEtc.getFullCount() > 0) {
                    SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
                    skuLadderEntity.setSkuId(skuDiscountsEtc.getSkuId());
                    skuLadderEntity.setFullCount(skuDiscountsEtc.getFullCount());
                    skuLadderEntity.setDiscount(skuDiscountsEtc.getDiscount());
                    skuLadderEntity.setAddOther(skuDiscountsEtc.getCountStatus());
                    skuLadderService.save(skuLadderEntity);
                }


                //sms_sku_fuLL_reduction
                if (skuDiscountsEtc.getFullPrice().compareTo(new BigDecimal(0)) > 0) {
                    SkuFullReductionEntity skuFullReduction = CopyBean.copyBean(skuDiscountsEtc, SkuFullReductionEntity.class);
                    skuFullReduction.setAddOther(skuDiscountsEtc.getPriceStatus());
                    this.save(skuFullReduction);
                }

                //sms_member_price 会员等级优惠信息
                /*if (CollUtil.isNotEmpty(skuDiscountsEtc.getMemberPrice())) {
                    List<MemberPriceEntity> memberPriceEntities = new ArrayList<>(skuDiscountsEtc.getMemberPrice().stream().map(po -> {
                        if (po.getPrice().compareTo(new BigDecimal(0)) > 0) {
                            MemberPriceEntity memberPrice = new MemberPriceEntity();
                            memberPrice.setSkuId(skuDiscountsEtc.getSkuId());
                            memberPrice.setMemberLevelId(po.getId());
                            memberPrice.setMemberLevelName(po.getName());
                            memberPrice.setMemberPrice(po.getPrice());
                            memberPrice.setAddOther(1);

                            return memberPrice;
                        }
                        return null;
                    }).toList());
                    memberPriceEntities.removeAll(Collections.singleton(null));
                    memberPriceService.saveBatch(memberPriceEntities);
                }*/
            }
            return true;
        }
        return false;
    }
}